<?php

App::uses('CrudController', 'Controller');

class AnswersController extends CrudController
{
    public $_model = 'Answer';
    
    public function admin() {
        $this->layout = null;
        $args = func_get_args();
        $questionId = $args[0];

        $this->request->allowMethod('get');
        $appBoschA = new stdClass();
        $appBoschA->question_id = $questionId;
        $this->set('appBoschA', $appBoschA);
        $this->set('title', __('Respuestas'));
        $this->set('description', $this->getDescription($questionId));
    }
    
    private function getDescription($questionId) {
        $this->loadModel('Question');
        $this->Question->id = $questionId;
        $question = $this->Question->read();
        return $question['Question']['description'];
    }
    
    public function uploadFiles() {
        $upload_dir = TMP;
        $path = $upload_dir . DS . 'files';
        if (!file_exists($path)) {
            mkdir ($path, 0777, true);
            $sub_path = $path . DS . 'images';
            mkdir ($sub_path, 0777, true);
        }
        $path .= DS . 'images';
        $name = 'file.img';
        $file = $_FILES['img'];
        move_uploaded_file($file['tmp_name'], $path . DS . $file['name']);
        $this->Session->write($name . '.name', $file['name']);
        $this->Session->write($name . '.path', $path);
        $succes = '';
        $this->set(array('succes' => $succes, '_serialize' => 'succes'));
        $this->viewClass = 'Json';
    }
    
    public function deletedUploaded() {
        $upload_dir = TMP;
        $path = $upload_dir . DS . 'files';
        if($this->Session->check('file.img')) {
            $file = $this->Session->read('file.img');
            unlink($path . DS . 'images' . DS . $file['name']);
            $this->Session->delete("file.img");
        }
        $succes = '';
        $this->set(array('succes' => $succes, '_serialize' => 'succes'));
        $this->viewClass = 'Json';
    }

    protected function getRecords($source_id) {
        $m = $this->_model;
        $records = $this->$m->getEnabledBySource($source_id);
        for ($i = 0; $i < count($records); $i++ ) {
            $records[$i]['ok'] = $records[$i]['ok'] == '1' ? true : false;
        }
        return $records;
    }
    
    protected function afterCreate(&$newRecord) {
        $m = $this->_model;
        $this->$m->id = $newRecord['id'];
        $files = array();
        if($this->Session->check('file.img')) {
            $file = $this->Session->read('file.img');
            $filenameParts = explode('.', $file['name']);
            $ext = $filenameParts[1];
            $newName = $file['path'] . DS . $newRecord['id'] . '.' . $ext;
            rename($file['path'] . DS . $file['name'], $newName);
            $route = Router::url('/', true) . 'PHP_TEMP/files/images/' . $newRecord['id'] . '.' . $ext;
            $files['image'] = $route;
            $newRecord['img'] = $route;
            $this->Session->delete("file.img");
            $this->$m->save($files);
        }
        return true;
    }
    
    protected function afterUpdate($id, &$updatedRecord) {
        $m = $this->_model;
        $this->$m->id = $id;
        $reg = $this->$m->read();
        $files = array();
        if($this->Session->check('file.img')) {
            if ($reg['Answer']['image'] != null) {
                unlink(WWW_ROOT . str_replace(Router::url('/', true), '', $reg['Answer']['image']));
            }
            $file = $this->Session->read('file.img');
            $filenameParts = explode('.', $file['name']);
            $ext = $filenameParts[1];
            $newName = $file['path'] . DS . $id . '.' . $ext;
            rename($file['path'] . DS . $file['name'], $newName);
            $route = Router::url('/', true) . 'PHP_TEMP/files/images/' . $id . '.' . $ext;
            $files['image'] = $route;
            $updatedRecord['img'] = $route;
            $this->Session->delete("file.img");
            $this->$m->save($files);
        }
        return true;
    }
    
    protected function c($model) {
        return array(
            'text' => $model->text,
            'description' => $model->text,
            'question_id' => $model->src_id,
            'is_ok' => $model->ok ? 1 : 0,
            'img' => $model->img,
            'ok' => $model->ok
        );
    }

    protected function u($model) {
        return array(
            'text' => $model->text,
            'description' => $model->text,
            'is_ok' => $model->ok,
            'ok' => $model->ok,
            'img' => $model->img
        );
    }
    
    protected function id($model) {
        return $model->value;
    }
}